package dryven.util.crypt;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESDecryption {
	private AESGeneratedKey _key;
	private String _encryptedMessage;
	
	public AESDecryption(AESGeneratedKey key, String encryptedMessage) {
		super();
		_key = key;
		_encryptedMessage = encryptedMessage;
	}

	public String decrypt() {
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(_key.getKey(), "AES");
		    Cipher cipher = Cipher.getInstance("AES");
		    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		    
		    byte[] encrypted = Base64.decode(_encryptedMessage);
		    byte[] original = cipher.doFinal(encrypted);
		    
		    return new String(original);
		} catch (Exception e) {
			throw new RuntimeException("Unable to decrypt", e);
		}
	}
}
